<?php
/**
 * $Id:$
 *
 * FGS is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

class htmlTable {

	var $headers = array();
	var $current = array();
	var $fields = array();
	
	function htmlTable() {
	}

	function clear() {
		$this->current = array();
		$this->fields = array();
	}

	function setHeader($id, $params) {
		$this->headers[$id] = $params;
	}

	function set($id, $value) {
		$this->current[$id] = $value;
	}

	function setLineParam($key, $value) {
		$this->current['__LINE_PARAM__'][$key] = $value;
	}

	function attachLine($text) {
		$this->current['__LINE_ARRAY__'][] = $text;
	}

	function push() {
		$this->fields[] = $this->current;
		$this->current = array();
	}

	function render($width = '100%') {
		$buffer = array();
		if (count($this->current))
			$this->push;

		$buffer[] = sprintf('<table style="width: %s">', $width);
		$buffer[] = '<thead><tr>';
		foreach ($this->headers as $row) {
			$params = '';
			if (!empty($row['width']))
				$params .= sprintf(' style="width: %s"', $row['width']);
			$buffer[] = sprintf('<th%s>%s</th>', $params, $row['title']);
		}
		$buffer[] = '</tr></thead>';

		$buffer[] = '<tbody>';
		$i = 0;
		foreach ($this->fields as $row) {
			$i = ($i + 1) % 2;
			$buffer[] = '<tr';
			if (is_array($row['__LINE_PARAM__'])) {
				$p = array();
				foreach ($row['__LINE_PARAM__'] as $k=>$v)
					$p[] = sprintf('%s="%s"', $k, $v);
				$buffer[] = ' ' . implode(' ', $p);
			}
			$buffer[] = '>';
			foreach ($this->headers as $k=>$v) {
				$params = '';
				if (!empty($v['align']))
					$params .= sprintf(' align="%s"', $v['align']);
				$buffer[] = sprintf('<td class="t%d"%s>%s</td>', $i, $params, $row[$k]);
			}
			if (is_array($row['__LINE_ARRAY__'])) {
				foreach ($row['__LINE_ARRAY__'] as $v)
					$buffer[] = sprintf('<tr><td class="t%d" colspan="%d"%s>%s</td></tr>', $i, count($this->headers), $v);
			}
			$buffer[] = '</tr>';
		}
		$buffer[] = '</tbody>';
		$buffer[] = '</table>';

		return implode("\n", $buffer);
	}
	
}

?>
